/** 头插法实现反转链表 */
var reverseList = (head) => {
    /** 新链表的表头 */
    let pre = null;
    let cur = head;
    let next = head;

    while(cur){
        next = cur.next;
        cur.next = pre;
        pre = cur;
        cur = next;
    }
    return pre;
}

function ListNode(val, next = null){
    this.val = val;
    this.next = next;
}

let head = new ListNode(1, new ListNode(2, new ListNode(3)));

// 反转链表
let reversedHead = reverseList(head);

// 打印反转后的链表
let cur = reversedHead;
while(cur){
    console.log(cur.val);
    cur = cur.next;
}
